package UIFree.FX.pageturner
{
	import UIFree.uif_ecomponent;
	
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	public class pt_card3DTurner extends pt_base3DTurner
	{
		private var mats: Array;
		public function pt_card3DTurner(parent:uif_ecomponent, a_transition:Function=null, a_fps:Number=20, a_duration:Number=1500)
		{
			super(parent, a_transition, a_fps, a_duration);
			mats = new Array();
		}
		
		override protected function startFX(): void {
			addChild(startBitmap);
			addChild(endBitmap);
			endBitmap.alpha = 0;
			mats[0] = {mat:new Matrix3D(), bmp: startBitmap};
			mats[1] = {mat:new Matrix3D(), bmp: endBitmap};
			for (var i: int=0; i<2; i++)
				(mats[i].mat as Matrix3D).appendTranslation(mats[i].bmp.x, mats[i].bmp.y, mats[i].bmp.z);
		}
		
		override protected function endFX(): void {
			removeChild(startBitmap);
			removeChild(endBitmap);
		}
		
		override protected function updateFromState(): void {
			startBitmap.alpha = 1 - state;
			endBitmap.alpha = state;
			
			var sine: Number = Math.sin(state * Math.PI);
			
			for (var i: int=0; i<2; i++) {
				var mat: Matrix3D = mats[i].mat.clone();
				var s2: Point = new Point(mats[i].bmp.bitmapData.rect.width / 2, mats[i].bmp.bitmapData.rect.height / 2);
				
				mat.appendTranslation(-s2.x, -s2.y, sine * 400);
				mat.appendRotation(sine * 90, new Vector3D(0.5, 0, 0.5));
				mat.appendTranslation(s2.x, s2.y, sine * 400);
				mats[i].bmp.transform.matrix3D = mat;
			}
		}
	}
}